﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using VoicePhone.Common;
using System.Speech.Synthesis;

namespace VoicePhone.Controls
{
    public class FastCallButton : BaseButton
    {
        public static List<SolidColorBrush> Brushs = new List<SolidColorBrush>()
        {
            new SolidColorBrush(Color.FromRgb(189, 189, 115)),
            new SolidColorBrush(Color.FromRgb(94, 200, 236)),
            new SolidColorBrush(Color.FromRgb(107, 190, 22)),
            new SolidColorBrush(Color.FromRgb(192, 0, 0)),
            new SolidColorBrush(Color.FromRgb(0, 0, 0))
        };

        public int Number { get; set; }

        public SolidColorBrush DefaultBackground
        {
            get { return (SolidColorBrush)GetValue(DefaultBackgroundProperty); }
            set { SetValue(DefaultBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DefaultBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefaultBackgroundProperty =
            DependencyProperty.Register("DefaultBackground", typeof(SolidColorBrush), typeof(FastCallButton), new PropertyMetadata(DefaultBackgroundChanged));

        private static void DefaultBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var fastCallButton = d as FastCallButton;
            fastCallButton.Background = fastCallButton.DefaultBackground;
        }

        public string CallName
        {
            get { return (string)GetValue(CallNameProperty); }
            set { SetValue(CallNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FastCallName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CallNameProperty =
            DependencyProperty.Register("CallName", typeof(string), typeof(FastCallButton), new PropertyMetadata(""));


        public string CallNo1
        {
            get { return (string)GetValue(CallNo1Property); }
            set { SetValue(CallNo1Property, value); }
        }

        // Using a DependencyProperty as the backing store for CallNo1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CallNo1Property =
            DependencyProperty.Register("CallNo1", typeof(string), typeof(FastCallButton), new PropertyMetadata(""));

        public CallStatus CallStatus
        {
            get { return (CallStatus)GetValue(CallStatusProperty); }
            set { SetValue(CallStatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CallStatus.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CallStatusProperty =
            DependencyProperty.Register("CallStatus", typeof(CallStatus), typeof(FastCallButton), new PropertyMetadata(CallStatus.Idle, StatusChanged));


        public string WaitMessage
        {
            get { return (string)GetValue(WaitMessageProperty); }
            set { SetValue(WaitMessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CallFailedReason.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WaitMessageProperty =
            DependencyProperty.Register("WaitMessage", typeof(string), typeof(FastCallButton));


        public SolidColorBrush WaitMessageBrush
        {
            get { return (SolidColorBrush)GetValue(WaitMessageBrushProperty); }
            set { SetValue(WaitMessageBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WaitMessageBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WaitMessageBrushProperty =
            DependencyProperty.Register("WaitMessageBrush", typeof(SolidColorBrush), typeof(FastCallButton), new PropertyMetadata(Brushes.Red));


        public bool Concentration
        {
            get { return (bool)GetValue(ConcentrationProperty); }
            set { SetValue(ConcentrationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Concentration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConcentrationProperty =
            DependencyProperty.Register("Concentration", typeof(bool), typeof(FastCallButton), new PropertyMetadata(false));

        static Random random = new Random();
        private static void StatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var fastCallButton = d as FastCallButton;
            if (fastCallButton.CallStatus != CallStatus.Talking)
                fastCallButton.WaitMessage = "";
            switch (fastCallButton.CallStatus)
            {
                case CallStatus.Idle:
                    fastCallButton.Background = fastCallButton.DefaultBackground;
                    break;
                case CallStatus.LordCall:
                    fastCallButton.Background = Brushs[0];
                    break;
                case CallStatus.ToBeCalled:
                    fastCallButton.Background = Brushs[1];
                    break;
                case CallStatus.Talking:
                    fastCallButton.Background = Brushs[2];
                    if ((CallStatus)e.OldValue == CallStatus.ToBeCalled)
                        break;
                    Task.Run(new Action(() =>
                    {
                        SpeechSynthesizer Speech = new SpeechSynthesizer();
                        Speech.SelectVoiceByHints(VoiceGender.Female);
                        //System.Threading.Thread.Sleep(random.Next(100, 3000));
                        fastCallButton.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Speech.SpeakAsync(TextSpeakHelper.TextExChange(fastCallButton.CallName));
                        }));
                    }));
                    break;
                    //case CallStatus.Holding:
                    //    fastCallButton.Background = Brushs[3];
                    //    break;
                    //case CallStatus.Failed:
                    //    fastCallButton.Background = Brushs[4];
                    //break;
            }
        }

        public void Copy(FastCallButton callButton)
        {
            this.CallName = callButton.CallName;
            this.CallStatus = callButton.CallStatus;
            this.CallNo1 = callButton.CallNo1;
        }
    }

    public enum CallStatus
    {
        Idle,
        LordCall,
        ToBeCalled,
        Talking,
        Holding,
        Failed,
        Looking
    }
}
